package com.huluobo.a2107mvpimdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.a2107mvpimdemo.R;
import com.huluobo.a2107mvpimdemo.bean.ContactListItemBean;
import com.huluobo.a2107mvpimdemo.chat.ChatActivity;
import com.huluobo.a2107mvpimdemo.chat.view.MyChatActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LC on 2023/9/27.
 */
public class MyContactAdapter extends RecyclerView.Adapter<MyContactAdapter.MyContactViewHolder> {
    private List<ContactListItemBean> listItemBeans = new ArrayList<>();

    private Context context;

    public MyContactAdapter(List<ContactListItemBean> listItemBeans, Context context) {
        this.listItemBeans = listItemBeans;
        this.context = context;
    }

    @NonNull
    @Override
    public MyContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyContactViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact_list,
                parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyContactViewHolder holder, int position) {
        //设置item中的用户名
        String username = listItemBeans.get(position).getUsername();
        holder.username.setText(username);
        //设置是否显示首字符
        if (listItemBeans.get(position).isShowFirstLetter()) {
            holder.firstLetter.setVisibility(View.VISIBLE);
            holder.firstLetter.setText(listItemBeans.get(position).getFirstLetter() + "");
        } else {
            holder.firstLetter.setVisibility(View.GONE);
        }
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyChatActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("conversationId", username);
                bundle.putInt("chatType", 1);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (listItemBeans == null) {
            return 0;
        }
        return listItemBeans.size();
    }

    class MyContactViewHolder extends RecyclerView.ViewHolder {
        TextView username, firstLetter;
        LinearLayout linearLayout;

        public MyContactViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.item_username);
            firstLetter = itemView.findViewById(R.id.item_firstLetter);
            linearLayout = itemView.findViewById(R.id.item_ll);
        }
    }
}
